<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\Business
 *
 * @property int $id
 * @property int $cid 分类ID
 * @property string $name 合作商名称
 * @property string $phone 电话
 * @property string|null $image 图片
 * @property string $longitude 经度
 * @property string $latitude 纬度
 * @property string $address 所在地址
 * @property int $status 状态:0=正常,1=隐藏
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\BusinessCategory|null $category
 * @method static \Illuminate\Database\Eloquent\Builder|Business newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Business newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Business query()
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereCid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereLongitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Business whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Business extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'business';

    public function category(): HasOne
    {
        return $this->hasOne(BusinessCategory::class,"id","cid");
    }

    /**
     * 计算经纬度距离
     * @param $longitude
     * @param $latitude
     * @return string
     */
    public static function getDistanceField($longitude,$latitude): string
    {
        return "(6370856*acos(cos(radians({$latitude}))*cos(radians(latitude))*cos(radians(longitude)-radians({$longitude}))+sin(radians({$latitude}))*sin(radians(latitude))))";
    }


    public static function getList(): array
    {
        $keyword = request()->input('keyword');
        $longitude = request()->input('longitude',113.348717);
        $longitude = $longitude?:113.348717;
        $latitude = request()->input('latitude',23.02165);
        $latitude = $latitude?:23.02165;
        $distance = self::getDistanceField($longitude,$latitude);
        $cid = request()->input("cid",0);
        $pageSize = request()->input("pageSize",0);
        $list = self::query()
            ->select(["id","name","phone","image","address","longitude","latitude"])
            ->addSelect(DB::raw($distance . ' as distances'))
            ->when($cid,function ($query,$value){
                $query->where("cid",$value);
            })->when($keyword,function ($query,$value){
                $query->whereRaw("concat(name,address) like ?",["%$value%"]);
            })->where("status",0)->orderByDesc("id")->paginate($pageSize);
        foreach ($list as $item){
            $distances = $item->distances;
            if ($distances>1000){
                $item->distances = number_format($distances/1000,2)."km";
            }else{
                $item->distances = number_format($distances,2)."m";
            }
            $item->image = cdn_url($item->image);
        }
        return [
            'items'=> $list->items(),
            'total'=>  $list->total(),
            'lastPage'=> $list->lastPage()
        ];
    }


}
